package com.baihui.taobao.db;

import com.baihui.taobao.vo.BOrder;
import com.baihui.taobao.vo.BUser;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Kevin. User: HSZM1106-0312 Date: 12-1-15 Time: 上午9:23 所有的买家信息
 */
public class UserDAO extends GeneDAO {

	@Override
	protected String getPrimaryKey(Class aClass) {
		return "userId";
	}

	@Override
	protected Class getDomainClass() {
		return BUser.class;
	}

	@Override
	protected String parseRelationType(Field field) {
		return null;
	}

	@Override
	protected Object parseValue(Object value, Field field) {
		return value;
	}

	@Override
	public String[] getExcludefield() {
		return new String[] { "serialVersionUID", "buyerCredit",
				"sellerCredit", "location", "UNDEAL", "DEAL" };
	}

	@Override
	public List<Map> findAllObject(String sql) throws Exception {
		List<Map> mapList = super.findAllObject(sql);
		// TradeDAO tradeDAO = new TradeDAO();
		// MemberDAO memberDAO = new MemberDAO();
		// for (Map map : mapList) {
		// if (map.get("nick") != null) {
		// String nick = (String) map.get("nick");
		// String tradeSql = "SELECT * from tbtrade where buyerNick = '" + nick
		// + "' order by created desc ";
		// List<Map> tradeList = tradeDAO.findAllObject(tradeSql);
		// Map oneTrade = null;
		// if (tradeList != null && tradeList.size() > 0) {
		// oneTrade = tradeList.get(0);
		// }
		// if (oneTrade == null) oneTrade = new HashMap<String, Object>();
		// map.put("shipping", oneTrade);
		//
		//
		// String memberSql = "select * from tbcrmmember where buyerNick = '" +
		// nick + "' ";
		// Map<String, Object> members = memberDAO.findObject(memberSql);
		// if (members == null) members = new HashMap<String, Object>();
		// map.put("memberInfo", members);
		// }
		// }
		return mapList;
	}

	@Override
	public List<Map> findAllObject(String sql, int maxValue) throws Exception {
		List<Map> mapList = super.findAllObject(sql, maxValue);
		// TradeDAO tradeDAO = new TradeDAO();
		// MemberDAO memberDAO = new MemberDAO();
		// for (Map map : mapList) {
		// if (map.get("nick") != null) {
		// String nick = (String) map.get("nick");
		// String tradeSql = "SELECT * from tbtrade where buyerNick = '" + nick
		// + "' order by created desc ";
		// List<Map> tradeList = tradeDAO.findAllObject(tradeSql);
		// Map oneTrade = null;
		// if (tradeList != null && tradeList.size() > 0) {
		// oneTrade = tradeList.get(0);
		// }
		// if (oneTrade == null) oneTrade = new HashMap<String, Object>();
		// map.put("shipping", oneTrade);
		//
		//
		// String memberSql = "select * from tbcrmmember where buyerNick = '" +
		// nick + "' ";
		// Map<String, Object> members = memberDAO.findObject(memberSql);
		// if (members == null) members = new HashMap<String, Object>();
		// map.put("memberInfo", members);
		// }
		// }
		return mapList;
	}

	@Override
	public Map<String, Object> findObject(String sql) throws Exception {
		Map<String, Object> map = super.findObject(sql);
		if (map.get("nick") != null) {
			String nick = (String) map.get("nick");
			String shippingSQL = "SELECT * from tbshipping where buyerNick = '"
					+ nick
					+ "' and created = (select max(created) from tbshipping where buyerNick = '"
					+ nick + "')";
			ShippingDAO shippingDAO = new ShippingDAO();
			Map<String, Object> shipping = shippingDAO.findObject(shippingSQL);
			if (shipping == null)
				shipping = new HashMap<String, Object>();
			map.put("shipping", shipping);

			String memberSql = "select * from tbcrmmember where buyerNick = '"
					+ nick + "' ";
			MemberDAO memberDAO = new MemberDAO();
			Map<String, Object> members = memberDAO.findObject(memberSql);
			if (members == null)
				members = new HashMap<String, Object>();
			map.put("memberInfo", members);
		}
		return map;
	}

	public void insertUsers(List<BUser> infos, Connection cn) throws Exception {
		if (infos == null || infos.size() <= 0)
			return;
		StringBuffer sql = new StringBuffer();
		sql.append(" insert into tbuser(userType,city,state,buyerCredit_score,sellerCredit_score,buyerCredit_goodNum,buyerCredit_totalNum,sellerCredit_goodNum,sellerCredit_totalNum,");
		sql.append(" birthday,created,email,lastVisit,nick,sex,status,type,userId,uid,vipInfo,exported,sellerid,baihuiid )");
		sql.append(" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");

		List<Object[]> values = new ArrayList<Object[]>();
		for (BUser user : infos) {
			List<Object> tmp = new ArrayList<Object>();
			tmp.add(user.getUserType());
			tmp.add(user.getCity());
			tmp.add(user.getState());
			tmp.add(user.getBuyerCredit_score());
			tmp.add(user.getSellerCredit_score());
			tmp.add(user.getBuyerCredit_goodNum());
			tmp.add(user.getBuyerCredit_totalNum());
			tmp.add(user.getSellerCredit_goodNum());
			tmp.add(user.getSellerCredit_totalNum());
			tmp.add(user.getBirthday());
			tmp.add(user.getCreated());
			tmp.add(user.getEmail());
			tmp.add(user.getLastVisit());
			tmp.add(user.getNick());
			tmp.add(user.getSex());
			tmp.add(user.getStatus());
			tmp.add(user.getType());
			tmp.add(user.getUserId());
			tmp.add(user.getUid());
			tmp.add(user.getVipInfo());
			tmp.add(user.getExported());
			tmp.add(user.getSellerid());
			tmp.add(user.getBaihuiid());

			values.add(tmp.toArray(new Object[] {}));
		}

		updateAndInsert(cn, sql.toString(), values);
	}
}
